<?php
//商品分类
class CateAction extends BaseAction
{
	public function __construct()
    {
		parent::__construct();
		$this->menuid = !empty($_GET['menuid']) ? $_GET['menuid'] : 8;
	}
	//分类列表
    function Index()
    {
        $cate_mod = D('cate');
    	$result = $cate_mod->order('sort_order DESC')->findAll();
    	$cate_list = array();
    	foreach ($result as $val) {
    	    if ($val['pid']==0) {
    	        $cate_list['parent'][$val['cate_id']] = $val;
    	    } else {
    	        $cate_list['sub'][$val['pid']][] = $val;
    	    }
    	}

    	$this->assign('cate_list',$cate_list);
		$sub_menu = $this->admin_menu($this->menuid, true);
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=cate&a=add\', title:\''.L('add_cate').'\', width:\'550\', height:\'160\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_cate'));
		$this->assign('sub_menu',$sub_menu);
		$this->assign('big_menu',$big_menu);
		$this->display();
    }

    //添加分类数据
    function add()
    {
    	$cate_mod = D('cate');
    	$result = $cate_mod->order('sort_order DESC')->findAll();
    	$cate_list = array();
    	foreach ($result as $val) {
    	    if ($val['pid']==0) {
    	        $cate_list['parent'][$val['cate_id']] = $val;
    	    } else {
    	        $cate_list['sub'][$val['pid']][] = $val;
    	    }
    	}
    	$this->assign('cate_list',$cate_list);
        $this->display();
    }

    //添加分类
    function insert()
    {
    	$cate_mod = D('cate');
        if( false === $vo = $cate_mod->create() ){
	        $this->error( $cate_mod->error() );
	    }
	    $result = $cate_mod->where("cate_name='".$vo['cate_name']."'")->find();
	    if($result){
	        $this->error('该分类已经存在');
	    }
		//保存当前数据
	    $cate_id = $cate_mod->add();
	    $this->success(L('operation_success'), '', '', 'add');
    }

    function delete()
    {
        if((!isset($_GET['cate_id']) || empty($_GET['cate_id'])) && (!isset($_POST['cate_id']) || empty($_POST['cate_id']))) {
            $this->error('请选择要删除的分类！');
		}
		$cate_mod = D('cate');
		if (isset($_POST['cate_id']) && is_array($_POST['cate_id'])) {
		    $cate_ids = implode(',', $_POST['cate_id']);
		    $cate_mod->delete($cate_ids);
		} else {
		    $cate_id = intval($_GET['cate_id']);
		    $cate_mod->delete($cate_id);
		}
		$this->success(L('operation_success'));
    }

    function edit()
    {
		$cate_mod = D('cate');
		$cate_id = isset($_GET['cate_id']) && intval($_GET['cate_id']) ? intval($_GET['cate_id']) : $this->error(L('please_select'));
		$cate_info = $cate_mod->where('cate_id='.$cate_id)->find();

		$result = $cate_mod->order('sort_order DESC')->findAll();
    	$cate_list = array();
    	foreach ($result as $val) {
    	    if ($val['pid']==0) {
    	        $cate_list['parent'][$val['cate_id']] = $val;
    	    } else {
    	        $cate_list['sub'][$val['pid']][] = $val;
    	    }
    	}
	    $this->assign('cate_list', $cate_list);
	    $this->assign('cate_info',$cate_info);
	    $this->display();
    }

    public function update()
    {
        $cate_mod = D('cate');
        $old_cate = $cate_mod->where('cate_id='.$_POST['cate_id'])->find();
        //名称不能重复
        if ($_POST['cate_name'] != $old_cate['cate_name']) {
            if ($this->_cate_exists($_POST['cate_name'], $_POST['pid'], $_POST['cate_id'])) {
                $this->error('分类名称重复！');
            }
        }

        //获取此分类和他的所有下级分类id
        $vids = array();
        $children[] = $old_cate['cate_id'];
        $vr = $cate_mod->where('pid='.$old_cate['cate_id'])->findAll();
        foreach ($vr as $val) {
            $children[] = $val['cate_id'];
        }
        if (in_array($_POST['pid'], $children)) {
            $this->error('所选择的上级分类不能是当前分类或者当前分类的下级分类！');
        }

        $cate_mod->create();
		$result = $cate_mod->save();
		if(false !== $result){
			$this->success(L('operation_success'), '', '', 'edit');
		}else{
			$this->error(L('operation_failure'));
		}
    }

    private function _cate_exists($cate_name, $pid, $cate_id=0)
    {
        $result = D('cate')->where("cate_name='".$cate_name."' AND pid='".$pid."' AND cate_id<>'".$cate_id."'")->count();
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    public function sort_order()
    {
    	$cate_mod = D('cate');
		if (isset($_POST['listorders'])) {
            foreach ($_POST['listorders'] as $id=>$sort_order) {
            	$data['sort_order'] = $sort_order;
                $cate_mod->where('cate_id='.$id)->save($data);
            }
            $this->success(L('operation_success'));
        }
        $this->error(L('operation_failure'));
    }
}
?>